package com.example.controller;

import com.example.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 订单控制器
 * 演示另一个Controller的调用关系
 */
@RestController
@RequestMapping("/api/orders")
@Api(tags = "订单管理接口")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单接口
     * 调用OrderService.createOrder -> RedisUtil.set (直接调用)
     * 同时调用OrderService.createOrder -> UserService.updateUserStatus ->
     * UserService.cacheUserInfo -> RedisUtil.set (间接调用)
     */
    @PostMapping("/create")
    @ApiOperation(value = "创建订单", notes = "创建新订单并更新相关缓存")
    public String createOrder(@RequestParam String orderInfo) {
        String orderId = "order_" + System.currentTimeMillis();
        orderService.createOrder(orderId, orderInfo);
        return "订单创建成功: " + orderId;
    }

    /**
     * 取消订单接口
     * 调用OrderService.cancelOrder -> UserService.updateUserStatus ->
     * UserService.cacheUserInfo -> RedisUtil.set
     */
    @DeleteMapping("/cancel")
    @ApiOperation(value = "取消订单", notes = "取消订单并更新用户状态")
    public String cancelOrder(@RequestParam String orderId) {
        orderService.cancelOrder(orderId);
        return "订单已取消: " + orderId;
    }
}
